// 引入基地址
const baseURL = 'https://smart-shop.itheima.net/index.php?s=/api'

// 获取首页数据
function getIndexData(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/page/detail', // 使用全局配置文件中的baseURL拼接接口地址
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  });
}

// 获取商品搜索数据
function getGoodsList(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/goods/list',
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取所有分类
function getCategoryList(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/category/list',
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取商品详情
function getGoodsDetail(goods_Id, successCallback, failCallback) {
  // console.log(goods_Id,33333);
  wx.request({
    url: baseURL + `/goods/detail&goodsId=${goods_Id}`,
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取商品保障服务
function getGoodsService(goods_Id, successCallback, failCallback) {
  wx.request({
    url: baseURL + `/goods.service/list&goodsId=${goods_Id}`,
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取商品评价总数
function getAllProduction(goods_Id, successCallback, failCallback) {
  // console.log(goods_Id);
  wx.request({
    url: baseURL + `/comment/total&goodsId=${goods_Id}`,
    method: 'GET',
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取商品评价
function getProduction(goodsId, limit, successCallback, failCallback) {
  wx.request({
    url: baseURL + '/comment/listRows',
    method: 'GET',
    data: {
      goodsId,
      limit: parseInt(limit, 10)
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取商品评价列表
function getProductionList(goodsId, scoreType, page, successCallback, failCallback) {
  wx.request({
    url: baseURL + '/comment/listRows',
    method: 'GET',
    data: {
      scoreType,
      goodsId,
      page
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取图形验证码
function getPcitureCode(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/captcha/image',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN'
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

// 获取短信验证码
function getPhoneCode(captchaCode, captchaKey, mobile, successCallback, failCallback) {
  wx.request({
    url: baseURL + '/captcha/sendSmsCaptcha',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN'
    },
    data: {
      form: {
        captchaCode: captchaCode,
        captchaKey: captchaKey,
        mobile: mobile
      }
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

//登录
function login(mobile, smsCode, successCallback, failCallback) {
  wx.request({
    url: baseURL + '/passport/login',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN'
    },
    data: {
      form: {
        isParty: false,
        mobile: mobile,
        partyData: {},
        smsCode: smsCode
      }
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

//获取用户信息
function getUserInfo(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/user/info',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}


//获取余额，积分，优惠券
function getUserAssets(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/user/assets',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

//获取商品状态数量(全部/待支付/待收货/待发货)
function getOrderTodoCounts(successCallback, failCallback) {
  wx.request({
    url: baseURL + '/order/todoCounts',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })
}

//获取订单列表
function getOrderList(dataType, page, successCallback, failCallback) {
  wx.request({
    url: baseURL + '/order/list',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: {
      dataType: dataType,
      page: page
    },
    success(res) {
      successCallback(res.data);
    },
    fail(err) {
      failCallback(err);
    }
  })

}
//添加购物车
function addCart(goodsId, goodsNum, successCallback) {
  wx.request({
    url: baseURL + '/cart/add',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: {
      goodsId: goodsId,
      goodsSkuId: "0",
      goodsNum: goodsNum
    },
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })

}
// 更新购物车
function resetCart(goodsId, goodsSkuId, goodsNum, successCallback) {
  wx.request({
    url: baseURL + '/cart/update',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: {
      goodsId: goodsId,
      goodsSkuId: "0",
      goodsNum: goodsNum
    },
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })

    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 获取购物车列表
function getCartList(successCallback) {
  wx.request({
    url: baseURL + '/cart/list',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })

}
// 获取商品总数量
function getCartTotal(successCallback) {
  wx.request({
    url: baseURL + '/cart/total',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })

}
// 删除购物车商品
function clearCart(cartIds, successCallback) {
  wx.request({
    url: baseURL + '/cart/clear',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: {
      cartIds: cartIds
    },
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 获取订单结算信息
function checkOutOrder(data, successCallback) {
  wx.request({
    url: baseURL + '/checkout/order',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 提交订单
function checkOutSubmit(data, successCallback) {
  wx.request({
    url: baseURL + '/checkout/submit',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 支付订单
function orderPay(data, successCallback) {
  wx.request({
    url: baseURL + '/order/pay',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 取消订单
function orderCancel(data, successCallback) {
  wx.request({
    url: baseURL + '/order/cancel',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 确认收货
function orderConfirm(data, successCallback) {
  wx.request({
    url: baseURL + '/order/receipt',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 获取地址列表
function addressList(data, successCallback) {
  wx.request({
    url: baseURL + '/address/list',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}

// 添加新地址
function addressAdd(data, successCallback) {
  wx.request({
    url: baseURL + '/address/add',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}

// 删除地址
function addressRemove(data, successCallback) {
  wx.request({
    url: baseURL + '/address/remove',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 编辑地址
function addressEdit(data, successCallback) {
  wx.request({
    url: baseURL + '/address/edit',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 获取默认地址
function addressDefaultId(data, successCallback) {
  wx.request({
    url: baseURL + '/address/defaultId',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}

// 设置默认地址
function addressSetDefault(data, successCallback) {
  wx.request({
    url: baseURL + '/address/setDefault',
    method: 'POST',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
// 收货地址详情
function addressDetail(data, successCallback) {
  wx.request({
    url: baseURL + '/address/detail',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}

// 获取树
function regionTree(data, successCallback) {
  wx.request({
    url: baseURL + '/region/tree',
    method: 'GET',
    header: {
      platform: 'MP-WEIXIN',
      'Access-Token': wx.getStorageSync('token')
    },
    data: data,
    success(res) {
      wx.showToast({ title: res.message, icon: 'none' })
      successCallback(res.data);
    },
    fail(err) {
      wx.showToast({ title: err.message, icon: 'none' })
    }
  })
}
module.exports = {
  addressList,
  addressAdd,
  regionTree,
  addressRemove,
  addressEdit,
  addressDetail,
  addressSetDefault,
  addressDefaultId,
  getIndexData,
  getGoodsList,
  getCategoryList,
  getGoodsDetail,
  getGoodsService,
  getAllProduction,
  getProduction,
  getProductionList,
  getPcitureCode,
  login,
  getUserInfo,
  orderPay,
  orderCancel,
  orderConfirm,
  getUserAssets,
  getOrderTodoCounts,
  getPhoneCode,
  addCart,
  checkOutOrder,
  checkOutSubmit,
  resetCart,
  clearCart,
  getOrderList,
  getCartList,
  getCartTotal
}
